import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';

class HttpDioDemo extends StatefulWidget {
  HttpDioDemo({Key key}) : super(key: key);

  @override
  _HttpDioDemoState createState() => _HttpDioDemoState();
}

class _HttpDioDemoState extends State<HttpDioDemo> {

  List _list = [];

  @override
  void initState() {
    super.initState();
    this._getData();
  }

  _getData() async{
    var apiUrl = "http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&page=1";
    var dio = Dio();
    Response result = await dio.get(apiUrl);
    print(json.decode(result.data)['result']);

    setState(() {
      this._list = json.decode(result.data)['result'];
    });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Dio请求数据Demo'), 
      ), 
      body: this._list.length > 0 ? ListView(
        children: this._list.map((val){
          return ListTile(
            title: Text(val['title']),
          );
        }).toList(),
      ) : ('加载中...')
    );
  }
}